package com.topInterview;

public class leecode160 {
    public class ListNode {
        private int val;
        private ListNode next;
    }

    public ListNode getInterserctionNode(ListNode h1, ListNode h2) {
        int n = 0;
        ListNode cur1 = h1;
        ListNode cur2 = h2;
        while (cur1.next != null) {
            n++;
            cur1 = cur1.next;
        }

        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }

        if (cur1 != cur2) {
            return null;
        }

        cur1 = n > 0 ? h1 : h2;
        cur2 = cur1 == h1 ? h2 : h1;
        n = Math.abs(n);
        while (n != 0) {
            cur1 = cur1.next;
            n--;
        }

        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }
}
